/**
 * @description: 「档期详情」页面，目前用于「主持风采」模块查看和编辑档期详情功能
 * @author: Ruan Jiazhen
 * @date: 2024-03-27 15:21:47
 **/
import { reqAddSchedule, reqDeleteSchedule, reqEditSchedule, reqGetScheduleDetail } from '@/api/hosting-style';
import { TIME_INTERVAL } from '@/assets/constant/other-constants';
import dayjs from 'dayjs';
import bus from 'iny-bus';

let eventChannel: WechatMiniprogram.EventChannel;
Page({
  data: {
    radioIcon: {
      active: '/subpkg/assets/icon/radio-checked.svg',
      normal: '/subpkg/assets/icon/radio.svg',
    },
    checkBoxIcon: {
      active: '/subpkg/assets/icon/check-box-checked.svg',
      normal: '/subpkg/assets/icon/check-box.svg',
    },
    TIME_INTERVAL,
    isShowDatePicker: false,
    isShowCustomerInfo: true,
    isShowReceiptStatus: true,
    scheduleId: '',
    form: {
      address: '',
      customer_name: '',
      customer_phone: '',
      date: dayjs().format('YYYY-MM-DD'),
      paid: '',
      price: '',
      price_is_settle: '0',
      remark: '',
      schedule_type: '0',
      time_interval: TIME_INTERVAL[0],
    } as CreateScheduleReq,
  },
  onLoad(options) {
    eventChannel = this.getOpenerEventChannel();
    this.getScheduleDetail(options.scheduleId);
  },
  async getScheduleDetail(scheduleId: string) {
    this.setData({
      scheduleId,
    });
    try {
      const res = await reqGetScheduleDetail(scheduleId);
      if (res.code !== 200) {
        throw new Error(res.msg);
      }

      this.setData({
        form: {
          address: res.data.address,
          customer_name: res.data.customer_name,
          customer_phone: res.data.customer_phone,
          date: res.data.date,
          paid: res.data.paid,
          price: res.data.price,
          price_is_settle: res.data.price_is_settle,
          remark: res.data.remark,
          schedule_type: res.data.schedule_type,
          time_interval: res.data.time_interval,
        },
      });
    } catch (e: any) {
      wx.showToast({ title: e.message, icon: 'none' });
      console.error(e);
      wx.navigateBack();
    }
  },
  onClickDatePicker() {
    this.setData({
      isShowDatePicker: !this.data.isShowDatePicker,
    });
  },
  onScheduleDatePickerChange(e: WechatMiniprogram.CustomEvent) {
    this.setData({
      'form.date': e.detail.date,
      'form.time_interval': e.detail.time_interval,
    });
  },
  async onClickAddress() {
    try {
      const res = await wx.chooseLocation({});
      this.setData({
        'form.address': res.name,
      });
    } catch (e: any) {
      const errMsg = '获取地址失败，请检查网络或定位权限';
      wx.showToast({ title: errMsg, icon: 'none' });
    }
  },
  onChangeFormType(e: WechatMiniprogram.CustomEvent) {
    this.setData({
      'form.schedule_type': e.detail,
    });
  },
  onChangeReceiptStatus(e: WechatMiniprogram.CustomEvent) {
    this.setData({
      'form.price_is_settle': e.detail ? '1' : '0',
    });
  },
  onClickItemTitle(e: WechatMiniprogram.CustomEvent) {
    const showVar = e.currentTarget.dataset.showVar;
    this.setData({
      [showVar]: !this.data[showVar],
    });
  },
  onInput(e: WechatMiniprogram.Input) {
    const key = e.currentTarget.dataset.key;
    this.setData({
      [`form.${key}`]: e.detail.value,
    });
  },
  async onClickDelete() {
    try {
      const res = await wx.showModal({
        title: '提示',
        content: '确定删除档期？',
        showCancel: true,
        confirmColor: '#ff457a',
      });
      if (!res.confirm) return;

      const deleteRes = await reqDeleteSchedule(this.data.scheduleId);
      if (deleteRes.code !== 200) {
        throw new Error(deleteRes.msg);
      }
      wx.showToast({ title: '删除成功' });
      wx.navigateBack();
      bus.emit('deleteSchedule', this.data.scheduleId);
    } catch (e: any) {
      wx.showToast({ title: e.message, icon: 'none' });
    }
  },
  async onClickConfirm() {
    try {
      const res = await reqEditSchedule(this.data.scheduleId, this.data.form);
      if (res.code !== 200) {
        throw new Error(res.msg);
      }
      wx.showToast({ title: '编辑成功' });
      bus.emit('editSchedule');
    } catch (e: any) {
      wx.showToast({ title: e.message, icon: 'none' });
      console.error(e);
    }
  },
});
